File upload system

jamesperet 9 years ago
parent
commit
53caf2a148

+ 3 - 0
app/assets/javascripts/uploads.js.coffee

@@ -0,0 +1,3 @@
1
+# Place all the behaviors and hooks related to the matching controller here.
2
+# All this logic will automatically be available in application.js.
3
+# You can use CoffeeScript in this file: http://coffeescript.org/

+ 3 - 0
app/assets/stylesheets/uploads.css.less

@@ -0,0 +1,3 @@
1
+// Place all the styles related to the uploads controller here.
2
+// They will automatically be included in application.css.
3
+// You can use Less here: http://lesscss.org/

+ 5 - 0
app/controllers/admin_panel_controller.rb

@@ -7,6 +7,7 @@ class AdminPanelController < ApplicationController
7 7
   def dashboard
8 8
     @users = User.all
9 9
     @posts = BlogPost.all
10
+    @files = Upload.all
10 11
   end
11 12
 
12 13
   def posts
@@ -16,4 +17,8 @@ class AdminPanelController < ApplicationController
16 17
   def users
17 18
     @users = User.all
18 19
   end
20
+  
21
+  def files
22
+    @uploads = Upload.all
23
+  end
19 24
 end

+ 74 - 0
app/controllers/uploads_controller.rb

@@ -0,0 +1,74 @@
1
+class UploadsController < ApplicationController
2
+  before_action :set_upload, only: [:show, :edit, :update, :destroy]
3
+
4
+  # GET /uploads
5
+  # GET /uploads.json
6
+  def index
7
+    @uploads = Upload.all
8
+  end
9
+
10
+  # GET /uploads/1
11
+  # GET /uploads/1.json
12
+  def show
13
+  end
14
+
15
+  # GET /uploads/new
16
+  def new
17
+    @upload = Upload.new
18
+  end
19
+
20
+  # GET /uploads/1/edit
21
+  def edit
22
+  end
23
+
24
+  # POST /uploads
25
+  # POST /uploads.json
26
+  def create
27
+    @upload = Upload.new(upload_params)
28
+
29
+    respond_to do |format|
30
+      if @upload.save
31
+        format.html { redirect_to @upload, notice: 'Upload was successfully created.' }
32
+        format.json { render action: 'show', status: :created, location: @upload }
33
+      else
34
+        format.html { render action: 'new' }
35
+        format.json { render json: @upload.errors, status: :unprocessable_entity }
36
+      end
37
+    end
38
+  end
39
+
40
+  # PATCH/PUT /uploads/1
41
+  # PATCH/PUT /uploads/1.json
42
+  def update
43
+    respond_to do |format|
44
+      if @upload.update(upload_params)
45
+        format.html { redirect_to @upload, notice: 'Upload was successfully updated.' }
46
+        format.json { head :no_content }
47
+      else
48
+        format.html { render action: 'edit' }
49
+        format.json { render json: @upload.errors, status: :unprocessable_entity }
50
+      end
51
+    end
52
+  end
53
+
54
+  # DELETE /uploads/1
55
+  # DELETE /uploads/1.json
56
+  def destroy
57
+    @upload.destroy
58
+    respond_to do |format|
59
+      format.html { redirect_to uploads_url }
60
+      format.json { head :no_content }
61
+    end
62
+  end
63
+
64
+  private
65
+    # Use callbacks to share common setup or constraints between actions.
66
+    def set_upload
67
+      @upload = Upload.find(params[:id])
68
+    end
69
+
70
+    # Never trust parameters from the scary internet, only allow the white list through.
71
+    def upload_params
72
+      params.require(:upload).permit(:title, :file, :desciption)
73
+    end
74
+end

+ 2 - 0
app/helpers/uploads_helper.rb

@@ -0,0 +1,2 @@
1
+module UploadsHelper
2
+end

+ 5 - 0
app/models/upload.rb

@@ -0,0 +1,5 @@
1
+class Upload < ActiveRecord::Base
2
+  
3
+    mount_uploader :file, FileUploader
4
+  
5
+end

+ 68 - 0
app/uploaders/file_uploader.rb

@@ -0,0 +1,68 @@
1
+# encoding: utf-8
2
+
3
+class FileUploader < CarrierWave::Uploader::Base
4
+
5
+  # Include RMagick or MiniMagick support:
6
+  # include CarrierWave::RMagick
7
+  include CarrierWave::MiniMagick
8
+  include CarrierWave::MimeTypes
9
+
10
+  # Choose what kind of storage to use for this uploader:
11
+  if Rails.env.test? or Rails.env.cucumber?
12
+      storage :file
13
+  end
14
+
15
+  if Rails.env.development?
16
+      storage :file
17
+  end
18
+
19
+  if Rails.env.production?
20
+      # Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility if using fog: 
21
+      include Sprockets::Helpers::RailsHelper  
22
+      include Sprockets::Helpers::IsolatedHelper 
23
+      storage :fog
24
+  end
25
+
26
+  # Override the directory where uploaded files will be stored.
27
+  # This is a sensible default for uploaders that are meant to be mounted:
28
+  def store_dir
29
+    "uploads/#{mounted_as}/#{model.id}"
30
+  end
31
+  
32
+  def cache_dir
33
+    " ./tmp/uploads/#{mounted_as}/#{model.id}"
34
+  end
35
+
36
+  # Provide a default URL as a default if there hasn't been a file uploaded:
37
+  # def default_url
38
+  #   # For Rails 3.1+ asset pipeline compatibility:
39
+  #   # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
40
+  #
41
+  #   "/images/fallback/" + [version_name, "default.png"].compact.join('_')
42
+  # end
43
+
44
+  # Process files as they are uploaded:
45
+  # process :scale => [200, 300]
46
+  #
47
+  # def scale(width, height)
48
+  #   # do something
49
+  # end
50
+
51
+  # Create different versions of your uploaded files:
52
+  # version :thumb do
53
+  #   process :resize_to_fit => [50, 50]
54
+  # end
55
+
56
+  # Add a white list of extensions which are allowed to be uploaded.
57
+  # For images you might use something like this:
58
+  # def extension_white_list
59
+  #   %w(jpg jpeg gif png)
60
+  # end
61
+
62
+  # Override the filename of the uploaded files:
63
+  # Avoid using model.id or version_name here, see uploader/store.rb for details.
64
+  # def filename
65
+  #   "something.jpg" if original_filename
66
+  # end
67
+
68
+end

+ 2 - 0
app/views/admin_panel/_sidebar_nav.html.erb

@@ -4,6 +4,8 @@
4 4
 	   <%= link_to '<i class="icon-home icon-white"></i> Dashboard'.html_safe, admin_dashboard_path %></li>
5 5
 	   <% if current_page?(:action => 'posts')%><li class="active"> <% else %><li><% end %>
6 6
 	   <%= link_to '<i class="icon-file icon-white"></i> Posts'.html_safe, admin_posts_path %></li>
7
+	   <% if current_page?(:action => 'files')%><li class="active"> <% else %><li><% end %>
8
+	   <%= link_to '<i class="icon-file icon-white"></i> Files'.html_safe, admin_files_path %></li>
7 9
 	   <% if current_page?(:action => 'users')%><li class="active"> <% else %><li><% end %>
8 10
 	   <%= link_to '<i class="icon-user icon-white"></i> Users'.html_safe, admin_users_path %></li>
9 11
 	</ul>

+ 4 - 1
app/views/admin_panel/dashboard.html.erb

@@ -8,7 +8,10 @@
8 8
 		<div class="well summary">
9 9
 			<ul>
10 10
 				<li>
11
-					<a href="<%= admin_users_path %>"><span class="count"><%= @users.length %></span> Usuários</a>
11
+					<a href="<%= admin_users_path %>"><span class="count"><%= @users.length %></span> Users</a>
12
+				</li>
13
+				<li>
14
+					<a href="<%= admin_files_path %>"><span class="count"><%= @files.length %></span> Files</a>
12 15
 				</li>
13 16
 				<li class="last">
14 17
 					<a href="<%= admin_posts_path %>"><span class="count"><%= @posts.length %></span> Posts</a>

+ 35 - 0
app/views/admin_panel/files.html.erb

@@ -0,0 +1,35 @@
1
+<div class="row">
2
+	<%= render 'admin_panel/sidebar_nav' %>
3
+	<div class="span9">
4
+		<div class="page-header">
5
+		  <h1>Blog Posts <%= link_to 'Upload File', new_upload_path, :class => 'btn btn-primary btn-mini' %></h1>
6
+		</div>
7
+
8
+			<table class="table table-bordered">
9
+			  <thead>
10
+			    <tr>
11
+			      <th>Title</th>
12
+			      <th>File</th>
13
+			      <th>Desciption</th>
14
+			      <th></th>
15
+			      <th></th>
16
+			      <th></th>
17
+			    </tr>
18
+			  </thead>
19
+
20
+			  <tbody>
21
+			    <% @uploads.each do |upload| %>
22
+			      <tr>
23
+			        <td><%= upload.title %></td>
24
+			        <td><%= upload.file.to_s %></td>
25
+			        <td><%= upload.desciption %></td>
26
+			        <td><%= link_to 'Show', upload %></td>
27
+			        <td><%= link_to 'Edit', edit_upload_path(upload) %></td>
28
+			        <td><%= link_to 'Destroy', upload, method: :delete, data: { confirm: 'Are you sure?' } %></td>
29
+			      </tr>
30
+			    <% end %>
31
+			  </tbody>
32
+			</table>
33
+			
34
+	</div>
35
+</div>

+ 13 - 0
app/views/uploads/_form.html.erb

@@ -0,0 +1,13 @@
1
+<%= simple_form_for(@upload) do |f| %>
2
+  <%= f.error_notification %>
3
+
4
+  <div class="form-inputs">
5
+    <%= f.input :title %>
6
+    <%= f.file_field :file %>
7
+    <%= f.input :desciption %>
8
+  </div>
9
+
10
+  <div class="form-actions">
11
+    <%= f.button :submit %>
12
+  </div>
13
+<% end %>

+ 6 - 0
app/views/uploads/edit.html.erb

@@ -0,0 +1,6 @@
1
+<h1>Editing upload</h1>
2
+
3
+<%= render 'form' %>
4
+
5
+<%= link_to 'Show', @upload %> |
6
+<%= link_to 'Back', uploads_path %>

+ 4 - 0
app/views/uploads/index.json.jbuilder

@@ -0,0 +1,4 @@
1
+json.array!(@uploads) do |upload|
2
+  json.extract! upload, :id, :title, :file, :desciption
3
+  json.url upload_url(upload, format: :json)
4
+end

+ 5 - 0
app/views/uploads/new.html.erb

@@ -0,0 +1,5 @@
1
+<h1>New upload</h1>
2
+
3
+<%= render 'form' %>
4
+
5
+<%= link_to 'Back', uploads_path %>

+ 19 - 0
app/views/uploads/show.html.erb

@@ -0,0 +1,19 @@
1
+<p id="notice"><%= notice %></p>
2
+
3
+<p>
4
+  <strong>Title:</strong>
5
+  <%= @upload.title %>
6
+</p>
7
+
8
+<p>
9
+  <strong>File:</strong><br>
10
+  <%= image_tag @upload.file.to_s if @upload.file != nil %>
11
+</p>
12
+
13
+<p>
14
+  <strong>Desciption:</strong>
15
+  <%= @upload.desciption %>
16
+</p>
17
+
18
+<%= link_to 'Edit', edit_upload_path(@upload) %> |
19
+<%= link_to 'Back', admin_files_path %>

+ 1 - 0
app/views/uploads/show.json.jbuilder

@@ -0,0 +1 @@
1
+json.extract! @upload, :id, :title, :file, :desciption, :created_at, :updated_at

+ 5 - 0
config/routes.rb

@@ -1,5 +1,7 @@
1 1
 RailsWebsiteTemplate::Application.routes.draw do
2 2
   
3
+  resources :uploads
4
+
3 5
   get "admin/dashboard" => "admin_panel#dashboard", :as => :admin_dashboard
4 6
   get "admin" => "admin_panel#index"
5 7
   get "admin/posts" => "admin_panel#posts", :as => :admin_posts
@@ -8,6 +10,9 @@ RailsWebsiteTemplate::Application.routes.draw do
8 10
   get "blog" => "blog_posts#index", :as => :blog
9 11
   get "post/:id" => "blog_posts#show", :as => :post
10 12
   resources :blog_posts, path: '/admin/posts'
13
+  
14
+  get '/admin/files' => "admin_panel#files", :as => :admin_files
15
+  resources :uploads, path: '/admin/files'
11 16
 
12 17
   get "start/index"
13 18
   devise_for :users, :skip => [:sessions, :passwords, :confirmations, :registrations]

+ 11 - 0
db/migrate/20140922074342_create_uploads.rb

@@ -0,0 +1,11 @@
1
+class CreateUploads < ActiveRecord::Migration
2
+  def change
3
+    create_table :uploads do |t|
4
+      t.string :title
5
+      t.string :file
6
+      t.text :desciption
7
+
8
+      t.timestamps
9
+    end
10
+  end
11
+end

+ 9 - 1
db/schema.rb

@@ -11,7 +11,7 @@
11 11
 #
12 12
 # It's strongly recommended that you check this file into your version control system.
13 13
 
14
-ActiveRecord::Schema.define(version: 20140922054459) do
14
+ActiveRecord::Schema.define(version: 20140922074342) do
15 15
 
16 16
   # These are extensions that must be enabled in order to support this database
17 17
   enable_extension "plpgsql"
@@ -43,6 +43,14 @@ ActiveRecord::Schema.define(version: 20140922054459) do
43 43
   add_index "friendly_id_slugs", ["sluggable_id"], name: "index_friendly_id_slugs_on_sluggable_id", using: :btree
44 44
   add_index "friendly_id_slugs", ["sluggable_type"], name: "index_friendly_id_slugs_on_sluggable_type", using: :btree
45 45
 
46
+  create_table "uploads", force: true do |t|
47
+    t.string   "title"
48
+    t.string   "file"
49
+    t.text     "desciption"
50
+    t.datetime "created_at"
51
+    t.datetime "updated_at"
52
+  end
53
+
46 54
   create_table "users", force: true do |t|
47 55
     t.string   "email",                  default: "", null: false
48 56
     t.string   "encrypted_password",     default: "", null: false

BIN
public/uploads/file/1/_77631563_77631558.jpg


+ 1 - 1
readme.md

@@ -7,10 +7,10 @@ A template for creating rails websites that includes the following:
7 7
 * Basic Blog
8 8
 * Admin Panel
9 9
 * SummerNote editor
10
+* Image upload/File System
10 11
 
11 12
 Other features are still under development:
12 13
 
13
-* Image upload/File System
14 14
 * Translation (pt-BR, EN)
15 15
 * Email System
16 16
 * Search System

+ 49 - 0
test/controllers/uploads_controller_test.rb

@@ -0,0 +1,49 @@
1
+require 'test_helper'
2
+
3
+class UploadsControllerTest < ActionController::TestCase
4
+  setup do
5
+    @upload = uploads(:one)
6
+  end
7
+
8
+  test "should get index" do
9
+    get :index
10
+    assert_response :success
11
+    assert_not_nil assigns(:uploads)
12
+  end
13
+
14
+  test "should get new" do
15
+    get :new
16
+    assert_response :success
17
+  end
18
+
19
+  test "should create upload" do
20
+    assert_difference('Upload.count') do
21
+      post :create, upload: { desciption: @upload.desciption, file: @upload.file, title: @upload.title }
22
+    end
23
+
24
+    assert_redirected_to upload_path(assigns(:upload))
25
+  end
26
+
27
+  test "should show upload" do
28
+    get :show, id: @upload
29
+    assert_response :success
30
+  end
31
+
32
+  test "should get edit" do
33
+    get :edit, id: @upload
34
+    assert_response :success
35
+  end
36
+
37
+  test "should update upload" do
38
+    patch :update, id: @upload, upload: { desciption: @upload.desciption, file: @upload.file, title: @upload.title }
39
+    assert_redirected_to upload_path(assigns(:upload))
40
+  end
41
+
42
+  test "should destroy upload" do
43
+    assert_difference('Upload.count', -1) do
44
+      delete :destroy, id: @upload
45
+    end
46
+
47
+    assert_redirected_to uploads_path
48
+  end
49
+end

+ 11 - 0
test/fixtures/uploads.yml

@@ -0,0 +1,11 @@
1
+# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
2
+
3
+one:
4
+  title: MyString
5
+  file: MyString
6
+  desciption: MyText
7
+
8
+two:
9
+  title: MyString
10
+  file: MyString
11
+  desciption: MyText

+ 4 - 0
test/helpers/uploads_helper_test.rb

@@ -0,0 +1,4 @@
1
+require 'test_helper'
2
+
3
+class UploadsHelperTest < ActionView::TestCase
4
+end

+ 7 - 0
test/models/upload_test.rb

@@ -0,0 +1,7 @@
1
+require 'test_helper'
2
+
3
+class UploadTest < ActiveSupport::TestCase
4
+  # test "the truth" do
5
+  #   assert true
6
+  # end
7
+end